package com.leech.lambda;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class FunctionDemo {
    public static void main(String[] args) {
        Supplier<String> supplier = () -> "47";
        Predicate<String> isNumber = str -> str.matches("-?\\d+(\\.\\d+)?");

        //Function<String, Integer> change = str -> Integer.parseInt(str);
        Function<String, Integer> change = Integer::parseInt;

        Consumer<Integer> consumer = integer -> {
            if(integer % 2 == 0) {
                System.out.println("偶数："+integer);
            }else {
                System.out.println("奇数："+integer);
            }
        };

        mymethod(isNumber, supplier, consumer, change);
        mymethod( str -> str.matches("-?\\d+(\\.\\d+)?"), () -> "77a", System.out::println, str -> Integer.parseInt(str));

        //System.out.println(supplier.get());

        //System.out.println(isNumber.test("777a"));
    }

    private static void mymethod(Predicate<String> isNumber, Supplier<String> supplier, Consumer<Integer> consumer, Function<String, Integer> change) {
        if (isNumber.test(supplier.get())) {
            consumer.accept(change.apply(supplier.get()));
        }else {
            System.out.println("不是数字");
        }
    }
}
